home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 October: Mac OS SDK / Dev.CD Oct 96 SDK / Dev.CD Oct 96 SDK2.toast / Development Kits (Disc 2) / OpenDoc / OpenDoc Development / Debugging Support / OpenDoc Source Code / Storage / Bento / PrmRslvr.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-22  |  9.3 KB  |  340 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        PrmRslvr.cpp
  3.  
  4.     Contains:    Implementation of Storage Unit for the Mac Bento Container Suite.
  5.  
  6.     Owned by:    Vincent Lo
  7.  
  8.     Copyright:    © 1994 - 1995 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.         <12>     8/18/95    VL        1267187: Refcount sourcepart correctly.
  13.         <11>     6/29/95    VL        1242642: delete suView in CATCH block of
  14.                                     ResolvePromise.
  15.         <10>     6/16/95    VL        1259613: Fixed up ResolvePromise to handle
  16.                                     SetOffset on an unfocused storage unit.
  17.          <9>     5/26/95    VL        1251403: Multithreading naming support.
  18.          <8>     4/10/95    VL        1236490: Made GetPromiseValue work during
  19.                                     FulfillPromise.
  20.          <7>     3/14/95    VL        1225208,1225201: Added IsSettingPromise to
  21.                                     ensure that we don't call ResolvePromise
  22.                                     while setting a promise.
  23.          <6>    11/14/94    VL        1188257: Use Bento errors in BenotDef.h.
  24.          <5>     10/4/94    VL        1153379: Do not include SUPriv.h anymore.
  25.          <4>     9/29/94    RA        1189812: Mods for 68K build.
  26.          <3>     9/15/94    VL        1184871: Fixed ResolvePromise to handle
  27.                                     Drag and Drop correctly.
  28.          <2>     8/11/94    VL        1180299: Use modified CreateView.
  29.          <1>     7/26/94    VL        first checked in
  30.  
  31.     To Do:
  32. */
  33.  
  34. #ifndef _PRMRSLVR_
  35. #include "PrmRslvr.h"
  36. #endif
  37.  
  38. #ifndef SOM_CMStorageUnit_xh
  39. #include <CMSU.xh>
  40. #endif
  41.  
  42. #ifndef _ODMEMORY_
  43. #include <ODMemory.h>
  44. #endif
  45.  
  46. #ifndef __CM_API__
  47. #include <CMAPI.h>
  48. #endif
  49.  
  50. #ifndef _EXCEPT_
  51. #include <Except.h>
  52. #endif
  53.  
  54. #ifndef SOM_ODStorageUnitView_xh
  55. #include <SUView.xh>
  56. #endif
  57.  
  58. #ifndef SOM_ODStorageUnitCursor_xh
  59. #include <SUCursor.xh>
  60. #endif
  61.  
  62. #ifndef SOM_ODPart_xh
  63. #include <Part.xh>
  64. #endif
  65.  
  66. #ifndef SOM_ODSession_xh
  67. #include <ODSessn.xh>
  68. #endif
  69.  
  70. #ifndef SOM_ODDragAndDrop_xh
  71. #include <DragDrp.xh>
  72. #endif
  73.  
  74. #ifndef _BENTODEF_
  75. #include "BentoDef.h"
  76. #endif
  77.  
  78. #ifndef _ODDEBUG_
  79. #include "ODDebug.h"    // Adkins -- added
  80. #endif
  81.  
  82. //==============================================================================
  83. // PromiseResolver
  84. //==============================================================================
  85.  
  86. //------------------------------------------------------------------------------
  87. // PromiseResolver::IsPromiseValue
  88. //------------------------------------------------------------------------------
  89.  
  90. ODBoolean PromiseResolver::IsPromiseValue(Environment* ev)
  91. {        
  92.     CMValue value = fSU->GetCurValue(ev);
  93.     return (this->GetPromiseInfo(value) ? kODTrue : kODFalse);
  94. }
  95.  
  96. //------------------------------------------------------------------------------
  97. // PromiseResolver::SetSourcePart
  98. //------------------------------------------------------------------------------
  99.  
  100. void PromiseResolver::SetSourcePart(Environment* ev,
  101.                                      ODPart *sourcePart)
  102. {
  103.     ODValueRefCon*        oldRefCon;
  104.     ODValueRefCon*        newRefCon;
  105.     CMValue                value;
  106.     ProcessSerialNumber thePSN;
  107.     
  108.     value = fSU->GetCurValue(ev);
  109.  
  110.     OSErr result = GetCurrentProcess(&thePSN);
  111.     THROW_IF_ERROR(result);
  112.  
  113.     oldRefCon = (ODValueRefCon *) this->GetPromiseInfo(value);
  114.     if (oldRefCon == kODNULL)
  115.     {
  116.         this->IncrementPromiseCount();
  117.         newRefCon = (ODValueRefCon *) ODNewPtr(sizeof(ODValueRefCon),
  118.                                                 fSU->GetHeap(ev));
  119.         ODAcquireObject(ev,    sourcePart);
  120.         newRefCon->sourcePart = sourcePart;
  121.         newRefCon->sourcePSN = thePSN;
  122.         this->SetPromiseInfo(value, newRefCon);
  123.     }
  124.     else if (oldRefCon->sourcePart != sourcePart)
  125.     {
  126.         oldRefCon->sourcePart = sourcePart;
  127.         oldRefCon->sourcePSN = thePSN;
  128.     }
  129. }
  130.  
  131. //------------------------------------------------------------------------------
  132. // PromiseResolver::GetSourcePart
  133. //------------------------------------------------------------------------------
  134.  
  135. ODPart* PromiseResolver::GetSourcePart(Environment* ev)
  136. {    
  137.     CMValue value = fSU->GetCurValue(ev);
  138.  
  139.     ODValueRefCon* theRefCon = (ODValueRefCon *) this->GetPromiseInfo(value);
  140.     if (theRefCon == kODNULL) {
  141.         if (fResolvingPromiseInfo)
  142.             theRefCon = fResolvingPromiseInfo;
  143.         else
  144.             THROW(kODErrNotPromise);
  145.     }
  146.     return theRefCon->sourcePart;
  147. }
  148.  
  149. //------------------------------------------------------------------------------
  150. // PromiseResolver::ClearPromise
  151. //------------------------------------------------------------------------------
  152.  
  153. void PromiseResolver::ClearPromise(Environment* ev)
  154. {
  155.     CMValue value = fSU->GetCurValue(ev);
  156.     if (value != kODNULL) {
  157.         ODPtr theRefCon = this->GetPromiseInfo(value);
  158.         if (theRefCon) {
  159.             ODPart* sourcePart = ((ODValueRefCon*) theRefCon)->sourcePart;
  160.             if (sourcePart)
  161.                 ODReleaseObject(ev, sourcePart);
  162.             ODDisposePtr(theRefCon);
  163.             this->SetPromiseInfo(value, kODNULL);
  164.             this->DecrementPromiseCount();
  165.         }
  166.     }
  167. }
  168.  
  169. //------------------------------------------------------------------------------
  170. // PromiseResolver::ResolvePromise
  171. //------------------------------------------------------------------------------
  172.  
  173. void PromiseResolver::ResolvePromise(Environment* ev)
  174. {
  175.     CMValue value = fSU->GetCurValue(ev);
  176.     ODValueRefCon* theRefCon = (ODValueRefCon *) this->GetPromiseInfo(value);
  177.     if (theRefCon)
  178.     {
  179.         ODStorageUnitView*    destSUView = kODNULL;
  180.         ODVolatile(destSUView);
  181.         TRY
  182.             fResolvingPromiseInfo = theRefCon;
  183.  
  184.             // First we must remove the refCon to stop any recursion
  185.             this->SetPromiseInfo(value, kODNULL);
  186.             
  187.             // Create a view for this storage unit
  188.             destSUView = fSU->CreateView(ev);
  189.             
  190.             ODDragAndDrop* dragAndDrop = fSU->GetSession(ev)->GetDragAndDrop(ev);
  191.             if (dragAndDrop->GetDragReference(ev) == 0) {
  192.                 Boolean             sameProcess;
  193.                 OSErr               result;
  194.                 ProcessSerialNumber thePSN;
  195.                 
  196.                 result = GetCurrentProcess(&thePSN);
  197.                 THROW_IF_ERROR(result);
  198.                 result = SameProcess(&(theRefCon->sourcePSN), &thePSN, &sameProcess);
  199.                 THROW_IF_ERROR(result);
  200.                 
  201.                 if (sameProcess) {
  202.                     ODPart*    sourcePart = theRefCon->sourcePart;
  203.                     if (sourcePart)
  204.                         sourcePart->FulfillPromise(ev, destSUView);
  205.                 }
  206.                 else {
  207.                     WARN("Cannot fulfill promise in another process.");
  208.                 }
  209.             }
  210.             else {    
  211.                 fSU->GetSession(ev)->GetDragAndDrop(ev)->GetPromiseFromDragManager(ev,
  212.                                                             theRefCon->sourcePart,
  213.                                                             destSUView);
  214.             }
  215.     
  216.             destSUView->SetOffset(ev, 0);
  217.             delete destSUView;
  218.             ODPart*    sourcePart = theRefCon->sourcePart;
  219.             if (sourcePart)
  220.             {
  221.                 ODReleaseObject(ev, sourcePart);
  222.             }
  223.             ODDisposePtr(theRefCon);
  224.             this->DecrementPromiseCount();
  225.         
  226.         CATCH_ALL
  227.         
  228.             if (destSUView)
  229.                 delete destSUView;
  230.  
  231.             fResolvingPromiseInfo = kODNULL;
  232.             if (ErrorCode() != kODErrUnfocusedStorageUnit)
  233.                 RERAISE;
  234.             
  235.         ENDTRY
  236.         
  237.         fResolvingPromiseInfo = kODNULL;
  238.     }
  239. }
  240.  
  241. //------------------------------------------------------------------------------
  242. // PromiseResolver::GetPromiseInfo
  243. //------------------------------------------------------------------------------
  244.  
  245. ODPtr PromiseResolver::GetPromiseInfo(CMValue curValue)
  246. {
  247.     return CMGetValueRefCon(curValue);
  248. }
  249.  
  250. //------------------------------------------------------------------------------
  251. // PromiseResolver::SetPromiseInfo
  252. //------------------------------------------------------------------------------
  253.  
  254. void PromiseResolver::SetPromiseInfo(CMValue curValue, ODPtr theInfo)
  255. {
  256.     CMSetValueRefCon(curValue, theInfo);
  257. }
  258.  
  259. //------------------------------------------------------------------------------
  260. // PromiseResolver::IncrementPromiseCount
  261. //------------------------------------------------------------------------------
  262.  
  263. void PromiseResolver::IncrementPromiseCount()
  264. {
  265.     fPromiseCount++;
  266. }
  267.  
  268. //------------------------------------------------------------------------------
  269. // PromiseResolver::DecrementPromiseCount
  270. //------------------------------------------------------------------------------
  271.  
  272. void PromiseResolver::DecrementPromiseCount()
  273. {
  274.     fPromiseCount--;
  275. }
  276.  
  277. //------------------------------------------------------------------------------
  278. // PromiseResolver::GetPromiseCount
  279. //------------------------------------------------------------------------------
  280.  
  281. ODULong PromiseResolver::GetPromiseCount()
  282. {
  283.     return fPromiseCount;
  284. }
  285.  
  286. //------------------------------------------------------------------------------
  287. // PromiseResolver::SettingPromise
  288. //------------------------------------------------------------------------------
  289.  
  290. void PromiseResolver::SettingPromise()
  291. {
  292.     fSettingPromise = kODTrue;
  293. }
  294.  
  295. //------------------------------------------------------------------------------
  296. // PromiseResolver::DoneSettingPromise
  297. //------------------------------------------------------------------------------
  298.  
  299. void PromiseResolver::DoneSettingPromise()
  300. {
  301.     fSettingPromise = kODFalse;
  302. }
  303.  
  304. //------------------------------------------------------------------------------
  305. // PromiseResolver::IsSettingPromise
  306. //------------------------------------------------------------------------------
  307.  
  308. ODBoolean PromiseResolver::IsSettingPromise()
  309. {
  310.     return fSettingPromise;
  311. }
  312.  
  313. //------------------------------------------------------------------------------
  314. // PromiseResolver::PromiseResolver
  315. //------------------------------------------------------------------------------
  316. PromiseResolver::PromiseResolver()
  317. {
  318.     fSU = kODNULL;
  319.     fPromiseCount = 0;
  320.     fSettingPromise = kODFalse;
  321.     fResolvingPromiseInfo = kODNULL;
  322. }
  323.  
  324. //------------------------------------------------------------------------------
  325. // PromiseResolver::~PromiseResolver
  326. //------------------------------------------------------------------------------
  327. PromiseResolver::~PromiseResolver()
  328. {
  329. }
  330.  
  331. //------------------------------------------------------------------------------
  332. // PromiseResolver::InitPromiseResolver
  333. //------------------------------------------------------------------------------
  334. void PromiseResolver::InitPromiseResolver(CMStorageUnit* su)
  335. {
  336.     fSU = su;
  337. }
  338.  
  339.  
  340.